<style>

</style>

<template>
  <p>
    <a-space>
        <TrainSelectView v-model="queryParam.trainCode" width="200px"></TrainSelectView>
        <a-button type="primary" @click="handleQuery()">查询</a-button>
        <a-button type="primary" @click="handleQuery()">刷新</a-button>
        <a-button type="primary" @click="onAdd">新增</a-button>
    </a-space>
    </p>
    <a-table 
        :dataSource="trainCarriages" 
        :columns="columns" 
        :pagination="pagination" 
        :loading="loading"
        @change="handleTableChange" 
    >
        <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'operation'">
                <a-space>
                    <a-popconfirm
                        title="删除后不可恢复，确认删除?"
                        @confirm="onDelete(record)"
                        ok-text="确认"
                        cancel-text="取消"
                    >
                        <a style="color: red">删除</a>
                    </a-popconfirm>
                
                    <a @click="onEdit(record)">编辑</a>
                </a-space>
            </template>
        </template>
    </a-table>
    <a-modal v-model:visible="visible" title="火车车厢" @ok="handleOk" ok-text="确认" cancel-text="取消">
        <a-form :model="train" :label-col="{span: 4}" :wrapper-col="{span: 14}">
            <a-form-item label="车次编号">
                <TrainSelectView v-model="trainCarriage.trainCode"></TrainSelectView>
            </a-form-item>
            <a-form-item label="箱号">
                <a-input v-model:value="trainCarriage.carriageIndex"/>
            </a-form-item>
            <a-form-item label="座位类型">
                <a-select v-model:value="trainCarriage.seatType">
                    <a-select-option value="1">一等座</a-select-option>
                    <a-select-option value="2">二等座</a-select-option>
                    <a-select-option value="3">软卧</a-select-option>
                    <a-select-option value="4">硬卧</a-select-option>
                </a-select>
            </a-form-item>
            <!-- <a-form-item label="座位数">
              <a-input v-model:value="trainCarriage.seatCount"/>
            </a-form-item> -->
            <a-form-item label="排数">
              <a-input v-model:value="trainCarriage.rowCount"/>
            </a-form-item>
            <!-- <a-form-item label="列数">
              <a-input v-model:value="trainCarriage.colCount"/>
            </a-form-item> -->
        </a-form>
    </a-modal>
</template>

<script>
import { defineComponent,ref,onMounted} from 'vue';
import { notification } from 'ant-design-vue';
import axios from 'axios';
import TrainSelectView from '@/components/TrainSelectView.vue';

export default defineComponent({
    name: "trainCarriage-view",
    components: { TrainSelectView },
    setup() {
        // 展示模态窗的标签的定义
        const visible = ref(false);
        const loading = ref(false);
        const queryParam = ref({
            trainCode: null
        });
        // 定义车站对象
        let trainCarriage = ref({
            id: undefined,
            trainCode: undefined,
            carriageIndex: undefined,
            seatType: undefined,
            seatCount: undefined,
            rowCount: undefined,
            colCount: undefined,
            createTime: undefined,
            updateTime: undefined
        });
        const trainCarriages = ref([]);
        const columns = [
            {
                title: '车次编号',
                dataIndex: 'trainCode',
                key: 'trainCode',
            },
            {
                title: '箱号',
                dataIndex: 'carriageIndex',
                key: 'carriageIndex',
            },
            {
                title: '座位类型',
                dataIndex: 'seatType',
                key: 'seatType',
            },
            {
                title: '座位数',
                dataIndex: 'seatCount',
                key: 'seatCount',
            },
            {
                title: '排数',
                dataIndex: 'rowCount',
                key: 'rowCount',
            },
            {
                title: '列数',
                dataIndex: 'colCount',
                key: 'colCount',
            },
            {
                title: '操作',
                dataIndex: 'operation',
                key: 'operation',
            }
        ];
        let pagination = ref({
            total: 0,
            current: 1,
            pageSize: 10,
        });
        // 点击新增展示模态窗的函数方法
        const onAdd = () => {
            console.log("add");
            visible.value = true;
            trainCarriage.value = {};
        };
        // 点击编辑展示模态窗的函数方法
        const onEdit = (record) => {
            console.log("edit");
            trainCarriage.value = window.Tool.copy(record);
            visible.value = true;
        };
        // 点击删除按钮的函数方法
        const onDelete = (record) => {
            axios.delete("/business/admin/train-carriage/delete/" + record.id)
                .then(response => {
                console.log(response.data.data);
                if (response.data.data) {
                    notification.success({ description: '删除成功' });
                    // 刷新页面
                    handleQuery();
                }
            })
                .catch(error => {
                notification.error({ description: error.response.data.data });
            });
        };

        // 点击模态窗的ok按钮执行的函数方法
        const handleOk = e => {
            switch (trainCarriage.value.seatType) {
                case "一等座":
                    trainCarriage.value.seatType = "1"
                    break;
                case "二等座":
                    trainCarriage.value.seatType = "2"
                    break;
                case "软卧":
                    trainCarriage.value.seatType = "3"
                    break;
                case "硬卧":
                    trainCarriage.value.seatType = "4"
                    break;
            
                default:
                    break;
            }
            axios.post("/business/admin/train-carriage/save", trainCarriage.value)
                .then(response => {
                let respData = response.data;
                console.log(respData);
                if (respData.data) {
                    notification.success({ description: '保存成功！' });
                    // 设置为false，关闭模态窗
                    visible.value = false;
                    // 调用列表查询的接口，刷新数据
                    handleQuery();
                }
                else {
                    notification.error({ description: respData.errorMessage });
                }
            })
                .catch(error => {
                let errData = error.response.data;
                console.log(errData);
                notification.error({ description: errData.data });
            });
        };
        // 车站信息列表查询的方法
        const handleQuery = e => {
            // 刷新时候不带参数的判断
            if (!e) {
                e = {
                    page: 1,
                    limit: pagination.value.pageSize
                };
            }
            // 开启loading效果
            loading.value = true;
            axios.get("/business/admin/train-carriage/query", {
                params: {
                    page: e.page,
                    limit: e.limit,
                    trainCode: queryParam.value.trainCode
                }
            })
                .then(response => {
                // 关闭loading效果
                loading.value = false;
                let respData = response.data;
                let trainCarriageDataList = respData.data.dataList;
                // 对车厢座位类型进行转换
                trainCarriageDataList.forEach(trainCarriage => {
                    switch (trainCarriage.seatType) {
                        case "1":
                            trainCarriage.seatType = "一等座";
                            break;
                        case "2":
                            trainCarriage.seatType = "二等座";
                            break;
                        case "3":
                            trainCarriage.seatType = "软卧";
                            break;
                        case "4":
                            trainCarriage.seatType = "硬卧";
                            break;
                    }
                });
                // 分页控件的设置
                let total = respData.data.total;
                let currentPage = respData.data.currentPage;
                let pageSize = respData.data.pageSize;
                trainCarriages.value = trainCarriageDataList;
                pagination.value.current = currentPage;
                pagination.value.pageSize = pageSize;
                pagination.value.total = total;
            })
                .catch(error => {
                // 关闭loading效果
                loading.value = false;
                let errData = error.response.data;
                console.log(errData);
                notification.error({ description: errData.data });
            });
        };
        // 页码的点击事件
        const handleTableChange = (pagination) => {
            console.log("pagination:", pagination.value);
            // 调用车站列表查询方法
            handleQuery({
                page: pagination.current,
                limit: pagination.pageSize
            });
        };
        // 钩子函数，页面初始化的时候执行
        onMounted(() => {
            handleQuery({
                page: pagination.value.current,
                limit: pagination.value.pageSize
            });
        });
        return {
            visible,
            loading,
            queryParam,
            trainCarriage,
            trainCarriages,
            columns,
            pagination,
            onAdd,
            onEdit,
            onDelete,
            handleOk,
            handleQuery,
            handleTableChange
        };
    },
});
</script>